<?php

namespace App\Observers\Train\Snap;


use App\Models\Task\SnapTasks\Repositories\CountSnapNumberService;
use App\Models\Task\SnapTasks\SnapTask;
use Illuminate\Support\Facades\Cache;

class SnapTaskObserver
{
    /**
     * Handle the User "created" event.
     *
     * @param SnapTask $task
     *
     * @return void
     */
    public function created(SnapTask $task)
    {
        // 创建时 参数不全 会导致缓存中的字段不全
        $count_snap_num = new CountSnapNumberService();
        $count_snap_num->addCount();
    }

    /**
     * Handle the User "updated" event.
     *
     * @param SnapTask $task
     *
     * @return void
     */
    public function updated(SnapTask $task)
    {
        $cache_key = "Train:SnapTask:{$task->task_id}";
        Cache::put($cache_key, $task->refresh(), config('trip.ttl.day'));
    }

    public function saved(SnapTask $task)
    {
        $cache_key = "Train:SnapTask:{$task->task_id}";
        Cache::put($cache_key, $task->refresh(), config('trip.ttl.day'));
    }
}
